package com.umeox.base

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View

abstract class AbsDialog(private val context: Context) {

    protected val dialogView: View by lazy {
        LayoutInflater.from(context)
            .inflate(getLayoutId(), null, false)
    }

    private val dialog by lazy {
        BaseDialog.CustomDialogParams() //单位dp
            .contentView(dialogView)
            .setGravity(getDialogGravity())
            .setBgTransparent(dialogBgIsTransparent())
            .setWindowSize(context.resources.displayMetrics.widthPixels, 0)
            .setWindowTranslate(getWindowTranslateX(), getWindowTranslateY())
            .setBgOutSizeAlpha(getBgOutSizeAlpha())
            .setAnimations(getAnimationsId())
            .build(context).apply {
                setCancelable(getCancelable())
            }
    }

    open fun getDialogGravity(): Int {
        return Gravity.CENTER
    }

    open fun dialogBgIsTransparent(): Boolean {
        return true
    }

    open fun getWindowTranslateX(): Int {
        return 0
    }

    open fun getWindowTranslateY(): Int {
        return 0
    }

    open fun getBgOutSizeAlpha(): Float {
        return 0.5f
    }

    open fun getCancelable(): Boolean {
        return true
    }

    open fun getAnimationsId(): Int {
        return 0
    }

    abstract fun getLayoutId(): Int

    fun setCancelable(cancelable: Boolean) {
        dialog.setCancelable(cancelable)
    }

    fun setGravity(gravity: Int) {
        dialog.window?.setGravity(gravity)
    }

    open fun showDialog() {
        dialog.show()
    }

    open fun dismissDialog() {
        if (dialog.isShowing) {
            dialog.dismiss()
        }
    }

    fun isShowing() = dialog.isShowing

    fun setCancelListener(cancel: () -> Unit){
        dialog.setOnCancelListener {
            cancel()
        }
    }
}